package cn.ysym.tcsy;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import cn.ysym.dao.SettingDao;
import cn.ysym.model.Setting;
import cn.ysym.util.CommonUtil;
import cn.ysym.util.HttpUtil;

public class CodeReplaceActivity extends AppCompatActivity {

    private SettingDao settingDao;

    private ProgressBar progressBar;
    private EditText etOldCode;
    private EditText etNewCode;

    private CodeReplcaeTask myTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code_replace);
        setTitle("标签替换");

        settingDao = new SettingDao(this);

        progressBar = findViewById(R.id.progressBarOnline);

        etOldCode = findViewById(R.id.etOldCode);
        etNewCode = findViewById(R.id.etNewCode);

        Button bOK = findViewById(R.id.bOK);
        bOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptDo();
            }
        });

        etOldCode.requestFocus();
    }

    private void attemptDo() {
        if (myTask != null) {
            return;
        }

        // Reset errors.
        etOldCode.setError(null);
        etNewCode.setError(null);

        String oldCode = etOldCode.getText().toString();
        String newCode = etNewCode.getText().toString();
        progressBar.setVisibility(View.VISIBLE);
        myTask = new CodeReplcaeTask(oldCode, newCode);
        myTask.execute((Void) null);
    }

    public class CodeReplcaeTask extends AsyncTask<Void, Void, String> {
        private String oldCode;
        private String newCode;

        public CodeReplcaeTask(String oldCode, String newCode) {
            this.oldCode = oldCode;
            this.newCode = newCode;
        }

        @Override
        protected String doInBackground(Void... params) {
            JSONObject req = new JSONObject();
            try {
                Setting userName = settingDao.query("userName");
                Setting sessionKey = settingDao.query("sessionKey");

                req.put("version", CommonUtil.getSoftVersion());
                req.put("account", userName.getSettingValue());
                req.put("oldCode", oldCode);
                req.put("newCode", newCode);
                req.put("sign", HttpUtil.genSign(req, sessionKey.getSettingValue()));
                String retStr = HttpUtil.postJson("replaceCode.sdo", req.toString());
                return retStr;
            } catch (Exception e) {
                Log.d("zqfw", e.getMessage());
            }

            return null;
        }

        @Override
        protected void onPostExecute(final String retStr) {
            myTask = null;
            progressBar.setVisibility(View.GONE);

            if (retStr != null) {
                try {
                    JSONObject retJson = new JSONObject(retStr);
                    if (retJson.getInt("result") == 1) {
                        Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();
                        etOldCode.setText("");
                        etNewCode.setText("");
                        etOldCode.requestFocus();
                    } else {
                        //CommonUtil.playSound(CodeReplaceActivity.this);
                        Toast.makeText(getApplicationContext(), retJson.getString("message"), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    Log.d("zqfw", e.getMessage());
                }
            }
        }

    }
}
